/* -*- mode: asm -*- */

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

        
        // Forward int sum, 2048 elements

.script_info {
  init : .function_id(main)
  traits : {
    .trait { kind:Trait_Method method: .function_id(setup)  name: setup }
    .trait { kind:Trait_Method method: .function_id(loop)   name: loop }
  }
}
        
function main()
{
        finddef setup
        callproperty setup (0)
        pop
        
        // Time stamp - before
        finddef Date
        constructprop Date (0)
        setlocal1
        
        finddef loop
        callproperty loop (0)
        pop

        // Time stamp - after
        finddef print
        finddef Date
        constructprop Date (0)
        getlocal1
        subtract
        pushstring "ms    MOPS forward int sum loop, 2048 elements"
        add
        callpropvoid print (1)

        returnvoid
}

function setup()
{
        // Construct the memory
        finddef {flash.utils}::ByteArray
        constructprop {flash.utils}::ByteArray 0
        dup
        setlocal1
        pushint 8192
        setproperty length

        // Initialize the memory to all "1" values
        getlocal1
        pushstring "littleEndian"
        setproperty endian
        
        pushint 0
        setlocal2
L1:     label
        getlocal2
        pushint 2048
        ifeq L2
        getlocal1
        pushint 1
        callproperty writeInt (1)
        pop
        inclocal 2
        jump L1
L2:     label
        getlocal1
        pushint 0
        setproperty position

        // Install the memory
        finddef {avmplus}::Domain
        getproperty {avmplus}::Domain
        getproperty currentDomain
        getlocal1
        setproperty domainMemory

        returnvoid
}
        
function loop()
{
        // Outer iteration counter
        // Outer loop from 0 to 10000
        pushint 0
        setlocal2

L1:     label
        getlocal2
        pushint 10000
        ifeq L3

        getlocal2
        increment_i
        coerce_i
        setlocal2

        // Inner iteration counter
        // Inner loop from 0 to 2048
        pushint 0
        setlocal1

        // Dummy sum
        pushint 0
        setlocal3
        
L2:     label
        getlocal1
        pushint 8192
        ifeq L1

        getlocal1
        li32
        getlocal3
        add
        coerce_i
        setlocal3
        
        getlocal1
        pushint 4
        add
        coerce_i
        setlocal1

        jump L2

L3:     label
        // Just check the sum
        getlocal3
        pushint 2048
        ifeq L4
        pushstring "MOPS loop forward: Unexpected result: "
        getlocal3
        add
        throw

L4:     label
        returnvoid
}
